<?php

namespace App\Supports\Payment\Channels;


use App\Supports\Payment\Channels\Sandpay\SandpaySdk\SandpayCommon;
use App\Supports\Payment\Channels\Sandpay\SandpaySdk\SandpayApiCommon;
use AdaPaySdk\Refund;
use AdaPaySdk\Payment;
use AdaPaySdk\AdapayTools;
use App\Supports\Payment\Contracts\Channel;
use App\Supports\Payment\Contracts\PayChannel;
use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;

class SandWap extends Channel implements PayChannel
{
	public function __construct()
	{
		$this->channelName = 'sandwap';
		
		$this->setConfig();
		$this->setOrigin();
	}
	
	public function setConfig()
	{
		$this->config = config('sandpay');
	}
	
	public function setOrigin()
	{
		// TODO: Implement setOrigin() method.
	}

	public function getRouteAction()
	{
		$routeAction = explode('@', Route::currentRouteAction());
		$action = $routeAction[1];
		return $action;
	}

	public function convertFormat($totalAmount)
	{
		$totalAmount = $totalAmount * 100;
        if(strlen($totalAmount) < 12){
        	$len = strlen($totalAmount);
        	for($i = 0; $i < (12 - $len); $i++){
				$totalAmount = '0'.$totalAmount;
        	}
        }
        return $totalAmount;

	}

	
	public function getChannelName()
	{
		return $this->channelName;
	}
	
	public function tools($data = null)
	{
		// TODO: Implement tools() method.
	}
	
	public function verify($data = null)
	{
		$client = new SandpayCommon($this->config);
		return $client->verify($data['data'], $data['sign']);
	}
	
	public function find($data = null)
	{
		// TODO: Implement find() method.
	}
	
	public function pay($data = null)
	{

		$action = $this->getRouteAction();
		$client = new SandpayCommon($this->config);
        // 参数
		$returnUrl = app()->environment('production')
						? 'https://wap.bjyike.com/#/packs/pages/order/paid'
						: 'https://dwap.bjyike.com/#/packs/pages/order/paid';
	
      	if($action=="payGoldChargeBySandwap"){
	        /*$client->productId = "05030001";
			$payMode = 'sand_h5';
			$totalAmount = $this->convertFormat($data['total_amount']);
			$body = array(
	            'orderCode'   => $data['out_trade_no'],
	            'totalAmount' => $totalAmount,
	            'subject'     => $data['subject'],
	            'body'        => $data['subject'],
	            'payMode'     => $payMode,
	            'payExtra'    => array(
              	  'cardNo' => $data['card_no'],
           		 ),
	            'limitPay'    => '1',
	            'clientIp'    => $_SERVER['REMOTE_ADDR'],
	            'notifyUrl'   => route('web.payments.sandpay.notify'),
	            'frontUrl'    => route('web.payments.sandpay.return'),	
	       	);
	       
	       	$client->body  = $body;
      	 	// 返回结果
       		$ret = $client->request('orderPay');*/

       		$client->productId = "05030001";
      		$clientIp = $_SERVER['REMOTE_ADDR'];
      		$clientIp = str_replace('.','_',$clientIp);

      		$body = array(
	            'mer_order_no'   => $data['out_trade_no'],
	            'order_amt' => $data['total_amount'],
	            'goods_name'     => $data['subject'],
	            'create_ip'    => $clientIp,
	            'pay_extra'    => array(
              	  'userId' => "ANFSM".$data['user_id'],
              	  'userName' => $data['user_name'],
              	  'idCard' => $data['card_no']
           		 ),
	            'notify_url'   => route('web.payments.sandpay.notify'),
	            'return_url'    => $returnUrl,	
	       	);
      	 	$ret = $client->wapPost($body);

      	}else{

      		$client->productId = "02010006";
      		$clientIp = $_SERVER['REMOTE_ADDR'];
      		$clientIp = str_replace('.','_',$clientIp);

      		$body = array(
	            'mer_order_no'   => $data['out_trade_no'],
	            'order_amt' => $data['total_amount'],
	            'goods_name'     => $data['subject'],
	            'create_ip'    => $clientIp,
	            'notify_url'   => route('web.payments.sandpay.notify'),
	            'return_url'    => $returnUrl,	
	       	);
      	 	$ret = $client->wxPost($body);
      	}

      
    	

		return $ret;
	}
	
	public function refund($data = null)
	{
		// TODO: Implement refund() method.
	}
	
	public function transfer($data = null)
	{
		// TODO: Implement transfer() method.
	}
	
	public function query($data = null)
	{
		// TODO: Implement query() method.
	}
}
